public class MyLinkList {

    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;


        public  ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //头插法
    public void addFirst(int data){
        //1.如果头插时 是第一个节点(即头节点 head 为 null)
        ListNode newlistNode = new ListNode(data);
        if(head == null){
            head = newlistNode;
            last = newlistNode;
            return;
        }

        //2. 正常头插
        head.prev = newlistNode;
        newlistNode.next = head;
        head = newlistNode;
        newlistNode.prev = null;
        return;
    }
    //尾插法
    public void addLast(int data){
        ListNode newlistNode = new ListNode(data);
        //1. 尾插时 作为第一个节点(即头节点 head 为 null)
        if(head == null){
            head = newlistNode;
            last = newlistNode;
            return;
        }

        //2. 正常尾插
        newlistNode.prev = last;
        last.next = newlistNode;
        last = newlistNode;
        newlistNode.next = null;
        return;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try{
            checkIndex(index);
        } catch (IndexisNotLegalException e){
            e.printStackTrace();
        }

        if(index == 0){
            addFirst(data);
        }

        if(index == size()){
            addLast(data);
        }

        ListNode cur = head;
        ListNode newlistNode = new ListNode(data);

        cur = findIndex(index);

        cur.next.prev = newlistNode;
        newlistNode.next = cur.next;
        cur.next = newlistNode;
        newlistNode.prev = cur;
        return;

    }

    private ListNode findIndex(int index){
        int count = index - 1;
        ListNode cur = head;
        while(count > 0){
            cur = cur.next;
            count--;
        }
        return cur;
    }

    private void checkIndex(int index) throws IndexisNotLegalException{
        if(index < 0 || index > size()){
            throw new IndexisNotLegalException("index下标异常");
        }
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //只有一个节点的情况下
                if(head == last){
                    head = null;
                    last = null;
                    return;
                }


                // 如果是 头节点
                if(cur == head){
                    head = head.next;
                    head.prev = null;
                }else if(cur == last){
                    last = last.prev;
                    last.next = null;
                }else{
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;

            }
            cur = cur.next;
        }
        return;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //如果只有一个节点的情况
                if(head == last){
                    head = null;
                    last = null;
                    return;
                }


                // 如果是 头节点
                if(cur == head){
                    head = head.next;
                    head.prev = null;
                }else if(cur == last){
                    //如果是 尾节点
                    last = last.prev;
                    last.next = null;
                }else{
                    //一般节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }

            }
            cur = cur.next;
        }
        return;

    }
    //得到双向链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        return;
    }
    public void clear(){
        ListNode cur =head;
        ListNode p = head;
        while(cur != null){
            cur = cur.next;
            p.prev = null;
            p.next = null;
            p = null;
            p = cur;
        }

        head = last = null;
        return;
    }

}

